package cn.neusoft.service.impl;

import cn.neusoft.mapper.CartItemMapper;
import cn.neusoft.pojo.CartItem;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.CartItemQuery;
import cn.neusoft.service.ICartItemService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class CartItemService implements ICartItemService {
	@Autowired
	private CartItemMapper cartItemMapper;
	@Transactional
	public ResultBean add(CartItem cartItem) throws Exception {
		if(cartItem.getBusinessId()==null ||
			cartItem.getBusinessId()==0 ||
			cartItem.getUserId()==null ||
		cartItem.getUserId()==0 ||
		cartItem.getFoodId()==null ||
		cartItem.getFoodId()==0) {
			return ResultBean.ERROR("请求参数为空");
		}
		//数据这里早好了
		//然后进行修改数据
		//1.先进性查询，查询的结果为空就正常添加
		//2.查询结果不为空，说明已经存在有这个对象，然后就进行修改其中数量这个属性的值。
		//3.最主要关键的是，查询语句的生成。
		CartItemQuery cartItemQuery=new CartItemQuery(cartItem.getFoodId(),
				cartItem.getBusinessId(),cartItem.getUserId());
		//这个条件写完之后，就开始写查询的条件了
		//这里不是写分页条件而是写一个独有的查询方法
		List<CartItem> list=cartItemMapper.getByCondition(cartItemQuery);
		System.out.println("===========================");
		for (CartItem cartitem:list) {
			System.out.println("订单详情："+cartitem);
		}
		CartItem target=null;
		int count=0;
		if(list.size()>0) {
			target = list.get(0);
			System.out.println("------------------"+target);
		}
		//这样写就可以拿到这个购物车的编号了
		//接下里就是一个多表联合查询，三个表进行查询，先在sql上写
		//多表分页查询加上条件查询
		if(target==null) {
			count=cartItemMapper.add(cartItem);
			return count>0?ResultBean.SUCCESS(cartItem.getCartId()):ResultBean.ERROR("添加购物车失败");
		} else {
			target.setQuantity(target.getQuantity()+ cartItem.getQuantity());
			System.out.println("------------"+target.getQuantity());
			System.out.println(target.getCartId());
			count=cartItemMapper.update(target);
			return count>0?ResultBean.SUCCESS(target.getCartId()):ResultBean.ERROR("修改购物车属性失败");
		}
	}
	@Transactional
	public ResultBean update(CartItem cartItem) throws Exception {
		if(cartItem.getBusinessId()==null ||
				cartItem.getBusinessId()==0 ||
				cartItem.getUserId()==null ||
				cartItem.getUserId()==0 ||
				cartItem.getFoodId()==null ||
				cartItem.getFoodId()==0 ||
			cartItem.getCartId()==null ||
			cartItem.getCartId()==0) {
			return ResultBean.ERROR("请求参数为空");
		}
		//这里一会先进行伪造数据然后再进行优化这里
		int count=cartItemMapper.update(cartItem);
		return count>0?ResultBean.SUCCESS(cartItem.getCartId()):ResultBean.ERROR("update is failed");
	}

	@Transactional
	public ResultBean delete(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		int count=cartItemMapper.delete(id);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("delete is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getById(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		CartItem cartItem = cartItemMapper.getById(id);
		return cartItem!=null?ResultBean.SUCCESS(cartItem):ResultBean.ERROR("getById is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getAll() throws Exception {
		List<CartItem>  list=cartItemMapper.getAll();
		return list!=null?ResultBean.SUCCESS(list):ResultBean.ERROR("getAll is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
		PageBean<CartItem> pageBean=new PageBean<CartItem>();
		if(baseQuery==null) {
			baseQuery=new CartItemQuery();
		}
		int count=cartItemMapper.getCount(baseQuery);
		pageBean.setTotalRows(count);
		//这里为什么不需要进行计算总页数和总记录数
		List<CartItem> list= cartItemMapper.getByPage(baseQuery);
		pageBean.setData(list);
		return ResultBean.SUCCESS(pageBean);
	}

	@Transactional
	public ResultBean batchDel(List<Integer> ids) throws Exception {
		if(ids==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		for ( Integer len: ids) {
			int delete = cartItemMapper.delete(len);
			if(delete==0) {
				System.out.println("删除购物车失败");
			}
		}
		return ResultBean.SUCCESS();
	}

	@Override
	public ResultBean getCartByBidAndUid(Integer userId, Integer businessId) {
		if(userId==null || userId==0 ||
		businessId==null || businessId==0) {
			return ResultBean.ERROR("请求参数为空");
		}
		CartItemQuery cartItemQuery=new CartItemQuery();
		cartItemQuery.setBusinessId(businessId);
		cartItemQuery.setUserId(userId);
		List<CartItem> cartItemList = cartItemMapper.getByCondition(cartItemQuery);
		return ResultBean.SUCCESS(cartItemList);
	}
}
